/**
 * Created by zheng on 2018/7/2.
 */
$(function () {
    $('#dg').datagrid({
        url: '/product/list',
        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
        //单选
        singleSelect:true,
        columns: [ [
            {field: 'x', checkbox: 'true'},
           /* {field: 'id', title: '编码', width: 100},*/
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '产品名称', width: 100},
            {
                field: 'orga', title: '机构名称', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "没有机构";
                }
            },
            {field: 'agelimit', title: '保障年限', width: 100},
            {field: 'coverage', title: '保额', width: 100},
            {field: 'annualfee', title: '年费', width: 100},
            {
                field: 'state', title: '保险状态', width: 100,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "<font color='green'>在售</font>";

                    } else if (value == 0) {
                        return "<font color='red'>停用</font>";
                    }
                }
            },
           {
                field: 'deductionstate', title: '不计免赔', width: 100,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "<font color='green'>是</font>";

                    } else if (value == 0) {
                        return "<font color='red'>否</font>";
                    }
                }
            }
        ]],
        //点击行时响应事件 保险状态
        onClickRow: function (index, row) {

            if (row.state == 1) {
                $("#changstate").linkbutton({
                    text: "停用"
                })
            } else {
                $("#changstate").linkbutton({
                    text: "在售"
                })
            }
            if (row.deductionstate == 1) {
                $("#deductionstate").linkbutton({
                    text: "否"
                })
            } else {
                $("#deductionstate").linkbutton({
                    text: "是"
                })
            }

        },
        //新增
        border:false,
        cls:"table table-very table-basic"
    })

    //准备输入的对话弹框
    $('#dd').dialog({
        title: 'My Dialog',
        width: 400,
        height: 400,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb"

    })

    //将工具栏所有的函数集合在一个对象中
    var cmdObj;
    cmdObj = {
        //刷新emp页面
        reload: function () {
            $('#dg').datagrid("load")
        },
        //添加
        add: function () {
            //清除以前的数据
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "机构信息添加")
        },
        //改变保险状态
        changState: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $.get("/product/changState", {id: row.id}, function (data) {
                if (data.succeed) {
                    $.messager.alert("温馨提示", "更新状态成功");
                    $('#dg').datagrid("reload")
                } else {
                    $.messager.alert("温馨提示", "删除状态失败");
                }
            })
        },
        //不计免赔状态
        deductionState: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $.get("/product/deductionState", {id: row.id}, function (data) {
                if (data.succeed) {
                    $.messager.alert("温馨提示", "更新状态成功");
                    $('#dg').datagrid("reload")
                } else {
                    $.messager.alert("温馨提示", "删除状态失败");
                }
            })
        },
        edit: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            //console.log(row);
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "机构信息编辑")
            //清除以前的数据
                //用于机构回显
            if (row.orga) {
                row["orga.id"] = row.orga.id;

            //回显
            $('#ff').form('load', row);


            }


        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
           // alert(1);
            var state = $("#state").combobox("getValue");
           // alert(2);
            var deductionstate = $("#deductionstate1").combobox("getValue");
          //  alert(3);
            $('#dg').datagrid('load', {
                keyword: keyword,
                state: state,
                deductionstate: deductionstate,
            });
        },
        //保存数据[按钮]
        save: function () {

            var url = "/product/saveOrUpdate";
            //提交表单
            $('#ff').form('submit', {
                url: url,

                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "保存成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#dg').datagrid("reload")
                    }
                }
            });
        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        }
    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})




